/******************************************
 * @Author:RNF
 * @Date:2024-07-28 00:24:30
 * @LastEditTime:2024-07-28 15:27:54
 * @LastEditors:XG
 * @Description:
 * @FilePath:\STM32Fans\User\button.c
 * @桃之夭夭
 ******************************************/
/**************************************************************************************************
* fuction	:                              button     
* file		:  button.c     
* brief		:     
* author	: rinanfang  3/2/2020
**************************************************************************************************/

#define __BUTTON_C__

/* ------------------------------------------- include ----------------------------------------- */
#include "button.h"


/* ------------------------------------- define/typedef/enum ----------------------------------- */


/* ------------------------------------------- variable ---------------------------------------- */
//Button's head
static struct button* Head_Button = NULL;

/* -------------------------------------------static funtion ----------------------------------------- */
//Name str copy
static char *StrnCopy(char *dst, const char *src, unsigned int n);
static void Button_Cycle_Process (button_t *btn);

//read sw level || sw event callback
////需要修改的函数声明
// static uint8_t 	Read_SW0_Level (void);

/* -------------------------------------------global funtion ----------------------------------------- */
//main button function
// extern void Button_Attach_ini (void);

/* -------------------------------------------modify funtion ----------------------------------------- */

button_t sw3;
button_t sw4;
button_t sw5;
button_t sw6;
button_t sw7;
button_t sw8;
button_t sw9;
button_t sw10;
button_t sw11;
button_t sw12;
button_t sw13;
button_t sw14;
button_t sw15;
button_t sw16;
button_t sw17;
button_t sw18;
button_t sw19;
//按键链接初始化
void Button_Attach_ini(void)
{

 	Button_Create("sw3",&sw3,read_sw3_level);
	Button_Attach(&sw3,BUTTON_DOWN,sw3_down_callBack);                       //call fuction attach
  Button_Create("sw4",&sw4,read_sw4_level);
  Button_Attach(&sw4,BUTTON_DOWN,sw4_down_callBack);
  Button_Create("sw5",&sw5,read_sw5_level);
  Button_Attach(&sw5,BUTTON_DOWN,sw5_down_callBack);
  Button_Create("sw6",&sw6,read_sw6_level);
  Button_Attach(&sw6,BUTTON_DOWN,sw6_down_callBack);
  Button_Create("sw7",&sw7,read_sw7_level);
  Button_Attach(&sw7,BUTTON_DOWN,sw7_down_callBack);
  Button_Create("sw8",&sw8,read_sw8_level);
  Button_Attach(&sw8,BUTTON_DOWN,sw8_down_callBack);
  Button_Create("sw9",&sw9,read_sw9_level);
  Button_Attach(&sw9,BUTTON_DOWN,sw9_down_callBack);
  Button_Create("sw10",&sw10,read_sw10_level);
  Button_Attach(&sw10,BUTTON_DOWN,sw10_down_callBack);
  Button_Create("sw11",&sw11,read_sw11_level);
  Button_Attach(&sw11,BUTTON_DOWN,sw11_down_callBack);
  Button_Create("sw12",&sw12,read_sw12_level);
  Button_Attach(&sw12,BUTTON_DOWN,sw12_down_callBack);
  Button_Create("sw13",&sw13,read_sw13_level);
  Button_Attach(&sw13,BUTTON_DOWN,sw13_down_callBack);
  Button_Create("sw14",&sw14,read_sw14_level);
  Button_Attach(&sw14,BUTTON_DOWN,sw14_down_callBack);
  Button_Create("sw15",&sw15,read_sw15_level);
  Button_Attach(&sw15,BUTTON_DOWN,sw15_down_callBack);
  Button_Create("sw16",&sw16,read_sw16_level);
  Button_Attach(&sw16,BUTTON_DOWN,sw16_down_callBack);
  Button_Create("sw17",&sw17,read_sw17_level);
  Button_Attach(&sw17,BUTTON_DOWN,sw17_down_callBack);
  Button_Create("sw18",&sw18,read_sw18_level);
  Button_Attach(&sw18,BUTTON_DOWN,sw18_down_callBack);
  Button_Create("sw19",&sw19,read_sw19_level);
  Button_Attach(&sw19,BUTTON_DOWN,sw19_down_callBack);

}
#include "../Core/Inc/gpio.h"
uint8_t read_sw3_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw4_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw5_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw6_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw7_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw8_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw9_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_11);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw10_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw11_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_14);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw12_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_15);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw13_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw14_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw15_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw16_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw17_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw18_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_15);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
uint8_t read_sw19_level(void)
{
    GPIO_PinState pinState = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13);
    return (pinState == GPIO_PIN_SET) ? 0 : 1;
}
double pwmx_duty_operation(double pwmx_duty, int add_sub_flag)
{
  double pwmx_duty_return = 0.0;
  int pwmx_duty_int = (int)(pwmx_duty*100); // 以 Hz 为单位的整数

  if (add_sub_flag == 1) // 增加
  {
    if (pwmx_duty_int == 1) 
    {
      pwmx_duty_int = 5; 
    }
    else
    {
      pwmx_duty_int += 5;
    }
  }
  else // 减少
  {
    if (pwmx_duty_int == 99) 
    {
      pwmx_duty_int = 95; 
    }
    else
    {
      pwmx_duty_int -= 5;
    }
  }
  // 限制频率范围
  if (pwmx_duty_int < 1)
  {
    pwmx_duty_int = 1; // 最小值 1%
  }
  if (pwmx_duty_int > 99)
  {
    pwmx_duty_int = 99; // 最大值 99%
  }
  pwmx_duty_return = (double)pwmx_duty_int / 100.0; // 转换回浮点数
  return pwmx_duty_return;
}
void sw3_down_callBack(void *btn)
{
  machine.pwm1_duty = pwmx_duty_operation(machine.pwm1_duty, machine.add_sub_flag); // 更新回浮点数
}
void sw4_down_callBack(void *btn)
{
  machine.pwm2_duty = pwmx_duty_operation(machine.pwm2_duty, machine.add_sub_flag); // 更新回浮点数
}
void sw5_down_callBack(void *btn)
{
  machine.pwm3_duty = pwmx_duty_operation(machine.pwm3_duty, machine.add_sub_flag); // 更新回浮点数
}
void sw6_down_callBack(void *btn)
{
  machine.pwm4_duty = pwmx_duty_operation(machine.pwm4_duty, machine.add_sub_flag); // 更新回浮点数
}
void sw7_down_callBack(void *btn)
{
  machine.pwm5_duty = pwmx_duty_operation(machine.pwm5_duty, machine.add_sub_flag); // 更新回浮点数
}
void sw8_down_callBack(void *btn)
{
  machine.pwm6_duty = pwmx_duty_operation(machine.pwm6_duty, machine.add_sub_flag); // 更新回浮点数
}
void sw9_down_callBack(void *btn)
{
  machine.pwm7_duty = pwmx_duty_operation(machine.pwm7_duty, machine.add_sub_flag); // 更新回浮点数
}
void sw10_down_callBack(void *btn)
{
  machine.pwm8_duty = pwmx_duty_operation(machine.pwm8_duty, machine.add_sub_flag); // 更新回浮点数
}
double pwmx_freq_operation(double pwmx_freq, int add_sub_flag)
{
  double pwmx_freq_return = 0.0;
  int pwmx_freq_int = (int)(pwmx_freq); // 以 Hz 为单位的整数

  if (add_sub_flag == 1) // 增加频率
  {
    if (pwmx_freq_int == 1*KHZ) // 1kHz
    {
      pwmx_freq_int = 100*KHZ; // 100kHz
    }
    else
    {
      pwmx_freq_int += 100*KHZ; // 增加 100kHz
    }
  }
  else // 减少频率
  {
    if (pwmx_freq_int == 100*KHZ) // 100kHz
    {
      pwmx_freq_int = 1*KHZ; // 1kHz
    }
    else
    {
      pwmx_freq_int -= 100*KHZ; // 减少 100kHz
    }
  }
  // 限制频率范围
  if (pwmx_freq_int < 1*KHZ)
  {
    pwmx_freq_int = 1*KHZ; // 最小值 1kHz
  }
  if (pwmx_freq_int > 3*MHZ)
  {
    pwmx_freq_int = 3*MHZ; // 最大值 3MHz
  }
  pwmx_freq_return = (double)(pwmx_freq_int); // 转换回浮点数
  return pwmx_freq_return;
}
void sw11_down_callBack(void *btn)
{
  machine.pwm1_freq = pwmx_freq_operation(machine.pwm1_freq, machine.add_sub_flag); // 更新回浮点数
  //sync other pwmx freq
  machine.pwm2_freq = machine.pwm1_freq; // 更新回浮点数
  machine.pwm3_freq = machine.pwm1_freq; // 更新回浮点数
  machine.pwm4_freq = machine.pwm1_freq; // 更新回浮点数
}
void sw12_down_callBack(void *btn)
{
  machine.pwm2_freq = pwmx_freq_operation(machine.pwm2_freq, machine.add_sub_flag); // 更新回浮点数
  //sync other pwmx freq
  machine.pwm1_freq = machine.pwm2_freq; // 更新回浮点数
  machine.pwm3_freq = machine.pwm2_freq; // 更新回浮点数
  machine.pwm4_freq = machine.pwm2_freq; // 更新回浮点数
}
void sw13_down_callBack(void *btn)
{
  machine.pwm3_freq = pwmx_freq_operation(machine.pwm3_freq, machine.add_sub_flag); // 更新回浮点数
  //sync other pwmx freq
  machine.pwm1_freq = machine.pwm3_freq; // 更新回浮点数
  machine.pwm2_freq = machine.pwm3_freq; // 更新回浮点数
  machine.pwm4_freq = machine.pwm3_freq; // 更新回浮点数
}
void sw14_down_callBack(void *btn)
{
  machine.pwm4_freq = pwmx_freq_operation(machine.pwm4_freq, machine.add_sub_flag); // 更新回浮点数
  //sync other pwmx freq
  machine.pwm1_freq = machine.pwm4_freq; // 更新回浮点数
  machine.pwm2_freq = machine.pwm4_freq; // 更新回浮点数
  machine.pwm3_freq = machine.pwm4_freq; // 更新回浮点数
}
void sw15_down_callBack(void *btn)
{
  machine.pwm5_freq = pwmx_freq_operation(machine.pwm5_freq, machine.add_sub_flag); // 更新回浮点数
  //sync other pwmx freq
  machine.pwm6_freq = machine.pwm5_freq; // 更新回浮点数
  machine.pwm7_freq = machine.pwm5_freq; // 更新回浮点数
  machine.pwm8_freq = machine.pwm5_freq; // 更新回浮点数
}
void sw16_down_callBack(void *btn)
{
  machine.pwm6_freq = pwmx_freq_operation(machine.pwm6_freq, machine.add_sub_flag); // 更新回浮点数
  //sync other pwmx freq
  machine.pwm5_freq = machine.pwm6_freq; // 更新回浮点数
  machine.pwm7_freq = machine.pwm6_freq; // 更新回浮点数
  machine.pwm8_freq = machine.pwm6_freq; // 更新回浮点数
}
void sw17_down_callBack(void *btn)
{
  machine.pwm7_freq = pwmx_freq_operation(machine.pwm7_freq, machine.add_sub_flag); // 更新回浮点数
  //sync other pwmx freq
  machine.pwm5_freq = machine.pwm7_freq; // 更新回浮点数
  machine.pwm6_freq = machine.pwm7_freq; // 更新回浮点数
  machine.pwm8_freq = machine.pwm7_freq; // 更新回浮点数
}
void sw18_down_callBack(void *btn)
{
  machine.pwm8_freq = pwmx_freq_operation(machine.pwm8_freq, machine.add_sub_flag); // 更新回浮点数
  //sync other pwmx freq
  machine.pwm5_freq = machine.pwm8_freq; // 更新回浮点数
  machine.pwm6_freq = machine.pwm8_freq; // 更新回浮点数
  machine.pwm7_freq = machine.pwm8_freq; // 更新回浮点数
}
void sw19_down_callBack(void *btn)
{
  if(machine.add_sub_flag == 1)
  {
    machine.add_sub_flag = -1;
  }
  else
  {
    machine.add_sub_flag = 1;
  }
} 
/* -------------------------------------------modify end ----------------------------------------- */


/* ------------------------------------------- funtion ----------------------------------------- */
//内部函数
/* ------------------------------------------- funtion ----------------------------------------- */
/************************************************************
  * @brief   按键周期处理函数
  * @param   btn:处理的按键
  * @return  NULL
  * @author  jiejie
  * @github  https://github.com/jiejieTop
  * @date    2018-xx-xx
  * @version v1.0
  * @note    必须以一定周期调用此函数，建议周期为20~50ms
  ***********************************************************/
void Button_Cycle_Process(button_t *btn)
{
  uint8_t current_level = (uint8_t)btn->Read_Button_Level();
  //按键电平发生变化，消抖
    if((current_level != btn->Button_Last_Level)&&(++(btn->level_timercnt) > BUTTON_LEVEL_STABLE)) 
    {
        btn->Button_Last_Level = current_level; //更新当前按键电平
        btn->level_timercnt = 0;                 //确定了是按下
          
         //如果按键是没被按下的，改变按键状态为按下(首次按下/双击按下)
        if(btn->Button_State == NONE_TRIGGER && btn->Button_Last_Level == btn->Button_Trigger_Level)
        {
            btn->Button_State = BUTTON_DOWN;
        }
        //释放按键
        else if(btn->Button_State == BUTTON_DOWN)
        {
            btn->Button_State = BUTTON_UP;
        }
        //双击检测
   #if SINGLE_AND_DOUBLE_TRIGGER == 1
        else if(btn->Button_State == BUTTON_DOUBLE_WAIT)
        {
            btn->Button_State = BUTTON_DOWN;
        }
    #endif
    }
    else if(current_level == btn->Button_Last_Level)
    {
        //重置消抖寄存器
        btn->level_timercnt = 0;
    }
  
  switch(btn->Button_State)
  {
    case BUTTON_DOWN :            // 按下状态
    {
	    //按键按下电平和我们设置电平相一致时
        if(btn->Button_Last_Level == btn->Button_Trigger_Level) 
        {
            //连按
            #if CONTINUOS_TRIGGER     

            if(++(btn->cycle_timercnt) >= BUTTON_CONTINUOS_CYCLE)
            {
              btn->cycle_timercnt = 0;
              btn->Button_Trigger_Event = BUTTON_CONTINUOS; 
              TRIGGER_CB(BUTTON_CONTINUOS);    //连按
              //PRINT_DEBUG("连按");
            }
            
            #else	    
    	    //按下定时器计数
    	    btn->down_timecnt++;
    	    
    	    //单击
            if (btn->down_timecnt < BUTTON_LONG_TIME && btn->Button_Trigger_Event != BUTTON_DOUBLE_WAIT)
            {
                btn->Button_Trigger_Event = BUTTON_DOWN; 
            }
    	    //长按释放
            #if LONG_FREE_TRIGGER == 1
            else if(btn->Long_Time == BUTTON_LONG_TIME)     
            {
                btn->Button_Trigger_Event = BUTTON_LONG_FREE; 
            } 
            #else
    	    //长按
            else if(btn->down_timecnt == BUTTON_LONG_TIME)      //释放按键前更新触发事件为长按
            {
                btn->Button_Trigger_Event = BUTTON_LONG; 
                TRIGGER_CB(BUTTON_LONG);                //第一次长按
            }
            
            //长按后
            else if(btn->down_timecnt > BUTTON_LONG_TIME)
            {
                  if(++(btn->cycle_timercnt) >= BUTTON_LONG_CYCLE)    //连续触发长按的周期
                  {
                    btn->cycle_timercnt = 0;
                    btn->Button_Trigger_Event = BUTTON_LONG; 
                    TRIGGER_CB(BUTTON_LONG);                        //长按连续触发
                  }
             }
            #endif
            //更新按下计数器时间  
            if(btn->down_timecnt == 0xFF)  
            {
                //更新长按后
                btn->down_timecnt = BUTTON_LONG_TIME+1;
            }
        }
          
      #endif
    

      break;
    } 
    
    case BUTTON_UP :        // 弹起状态
    {
            //触发单击
            if(btn->Button_Trigger_Event == BUTTON_DOWN)  
            {
                //单击
            #if (SINGLE_AND_DOUBLE_TRIGGER == 0)
                TRIGGER_CB(BUTTON_DOWN);    
                btn->down_timecnt=0;
                btn->Button_State = NONE_TRIGGER;
                btn->Button_Trigger_Event = NONE_TRIGGER;
            #else
                //双击检测
                if(btn->Button_Trigger_Event = BUTTON_DOWN)
                {
                    btn->down_timecnt=0;
                    btn->cycle_timercnt = 0;
                    btn->double_timecnt = 0;
                    btn->Button_State = BUTTON_DOUBLE_WAIT;
                    btn->Button_Trigger_Event = BUTTON_DOUBLE_WAIT;
                }
                esle if(btn->Button_Trigger_Event = BUTTON_DOUBLE_WAIT)
                {
                    //双击检测
                    btn->down_timecnt=0;
                    btn->cycle_timercnt = 0;
                    btn->double_timecnt = 0;
                    TRIGGER_CB(BUTTON_DOUBLE); 
                    btn->Button_Last_State = BUTTON_DOUBLE;
                    btn->Button_State = NONE_TRIGGER;
                    btn->Button_Trigger_Event = NONE_TRIGGER;
                }
            #endif 
            }
            else if(btn->Button_Trigger_Event == BUTTON_CONTINUOS)
            {
                btn->down_timecnt = 0;
                btn->cycle_timercnt = 0;
                //在按下那边已经触发了
                btn->Button_State = NONE_TRIGGER;
                btn->Button_Trigger_Event = NONE_TRIGGER;

            }
            //长按
            else if(btn->Button_Trigger_Event == BUTTON_LONG)
            {
                TRIGGER_CB(BUTTON_LONG_FREE);
                btn->down_timecnt = 0;
                btn->cycle_timercnt = 0;
                //在按下那边已经触发了
                btn->Button_State = NONE_TRIGGER;
                btn->Button_Trigger_Event = NONE_TRIGGER;
            }
            #if LONG_FREE_TRIGGER == 1
            //长按释放
            else if(btn->Button_Trigger_Event == BUTTON_LONG_FREE)
            {
                TRIGGER_CB(BUTTON_LONG_FREE);
                btn->down_timecnt = 0;
                btn->cycle_timercnt = 0;
                //在按下那边已经触发了
                btn->Button_Last_State = BUTTON_LONG_FREE;
                btn->Button_State = NONE_TRIGGER;
                btn->Button_Trigger_Event = NONE_TRIGGER;
            }
            #endif
            #if CONTINUOS_TRIGGER
            else if(btn->Button_Trigger_Event == BUTTON_CONTINUOS)  //连按
            {
              btn->Long_Time = 0;
              TRIGGER_CB(BUTTON_CONTINUOS_FREE);    //连发释放
              btn->Button_State = NONE_TRIGGER;
              btn->Button_Last_State = BUTTON_CONTINUOS;
            } 
            #endif
      
      break;
    }
    //双击检测
    #if (SINGLE_AND_DOUBLE_TRIGGER == 1)
    case BUTTON_DOUBLE_WAIT :		//双击中间间隔的超时判断
    {
        btn->double_timecnt++;     //时间记录 
        //超时
        if(btn->double_timecnt>=BUTTON_DOUBLE_TIME)
        {
            btn->double_timecnt = 0;
            btn->Button_Trigger_Event = BUTTON_DOWN;
            TRIGGER_CB(BUTTON_DOWN);
            btn->Button_State = NONE_TRIGGER;
            btn->Button_Trigger_Event = NONE_TRIGGER;
        }
      break;
    }
    #endif

    
    default :
      break;
  }

}

/**************************************************************************************************
* function	:                                   Button_Process
* brief	:  			   scan each button and each button's each button event
* param	:  			   none
* return			:  none
* author			:  rinanfang  3/2/2020
**************************************************************************************************/
void Button_Process(void)
{
  struct button* pass_btn;
  for(pass_btn = Head_Button; pass_btn != NULL; pass_btn = pass_btn->Next)
  {
      Button_Cycle_Process(pass_btn);
  }
}

/**************************************************************************************************
* function	:                                   Add_Button
* brief	:   add button node to button linear list
* param	:  button node
* return			:  none
* author			:  rinanfang  3/2/2020
**************************************************************************************************/
void Add_Button(button_t* btn)
{
  struct button *pass_btn = Head_Button;
  
  while(pass_btn)
  {
    pass_btn = pass_btn->Next;
  }
  
  btn->Next = Head_Button;
  Head_Button = btn;
}

/**************************************************************************************************
* function	:                                   Button_Delete
* brief	:  delete button node from button linear list
* param	:  button node
* return			:  none
* author			:  rinanfang  3/2/2020
**************************************************************************************************/
void Button_Delete(button_t *btn)
{
  struct button** curr;
  for(curr = &Head_Button; *curr;) 
  {
    struct button* entry = *curr;
    if (entry == btn) 
    {
      *curr = entry->Next;
    } 
    else
    {
      curr = &entry->Next;
    }
  }
}
/**************************************************************************************************
* function	:                                   Button_Create
* brief	:  create a new button nod and add it to button linear list
* param	:  button name||button node||button level||button trigger level		
* return			:  TRUE FALSE
* author			:  rinanfang  3/2/2020
**************************************************************************************************/
 boolean_t Button_Create(const char *name,
                  button_t *btn, 
                  uint8_t(*read_btn_level)(void))
{
  if( btn == NULL)
  {
	  return FALSE;
  }
  //"string.h"
  memset(btn, 0, sizeof(struct button));  //
  StrnCopy(btn->Name, name, BTN_NAME_MAX);
  
  //set button param to no trigger status
  btn->Button_State = NONE_TRIGGER;           //按键状态
  btn->Button_Trigger_Event = NONE_TRIGGER;   //按键触发事件
  btn->Read_Button_Level = read_btn_level;    //按键读电平函数
//	  btn->Button_Trigger_Level = HIGHT;  //按键触发电平
//	  btn->Button_Last_Level = LOW; 			//按键当前电平
  btn->Button_Trigger_Level = HIGHT;  //按键触发电平
  btn->Button_Last_Level = btn->Read_Button_Level(); //按键当前电平
	//debug_printf("btn:%d",btn->Button_Last_Level);
  btn->level_timercnt = 0;
	
  //add to line tabe
  Add_Button(btn);          //add button line
  
  return TRUE;
}
/**************************************************************************************************
* function	:                                   Button_Attach
* brief	:		attach button event to  corresponding button node
* param	:  		button node||button event's array idex||button event's callback
* return			:  TRUE FALSE
* author			:  rinanfang  3/2/2020
**************************************************************************************************/
 boolean_t Button_Attach(button_t *btn,Button_Event btn_event,Button_CallBack btn_callback)
{
  uint8_t i=0;
  if( btn == NULL)
  {
	  return FALSE;
  }
  
  if(BUTTON_ALL_RIGGER == btn_event)
  {
    for( i = 0 ; i < number_of_event-1 ; i++)
      btn->CallBack_Function[i] = btn_callback; //
  }
  else
  {
    btn->CallBack_Function[btn_event] = btn_callback; //
  }
  return TRUE;
}

/**************************************************************************************************
* function	:                                   StrnCopy
* brief	:  
* param	:  dst str || src str || str line
* return			:  dst str 
* author			:  rinanfang  3/2/2020
**************************************************************************************************/
static char *StrnCopy(char *dst, const char *src, uint32_t n)
{
  if (n != 0)
  {
    char *d = dst;
    const char *s = src;
    do
    {
        if ((*d++ = *s++) == 0)
        {
            while (--n != 0)
                *d++ = 0;
            break;
        }
    } while (--n != 0);
  }
  return (dst);
}



